//
//  HomeViewController.m
//  mafengwo
//
//  Created by Mac on 16/3/9.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "HomeSectionHeaderView.h"
#import "MonthModel.h"
@interface HomeViewController (){

    UIRefreshControl* re;//下拉刷新控件

}

@end

@implementation HomeViewController
- (id)init{
    self = [super init];
    if (self != nil) {
        //不闪一下的button
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchDown];
//
//        btn.frame = CGRectMake(100, 100, 200, 200);
////        btn.backgroundColor = [UIColor redColor];
//
//        [btn setTitle:@"ttttt" forState:UIControlStateNormal];
//        [btn setTitle:@"aaaaa" forState:UIControlStateHighlighted];
////        [self.view addSubview:btn];

        self.view.backgroundColor = [UIColor whiteColor];
        self.tabBarItem.image = [UIImage imageNamed:@"home_on.png"];
    }

    return self;
}

//不闪一下的button的Action
- (void)btn{

    NSLog(@"---");


}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self createTableView];

}

-(void)createTableView{

    //创建首页tableview
    UITableView *_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, KScreenW, KScreenH-49-21) style:UITableViewStylePlain];

    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 200;
    _tableView.sectionHeaderHeight = 80;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyboard)];
    [_tableView addGestureRecognizer:tap];
    [self.view addSubview:_tableView];

    //头视图
    UIView *_headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 200)];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _headerView.frame.size.width, _headerView.frame.size.height)];
    [imageV setImage:[UIImage imageNamed:@"home_header_image"]];
    [_headerView addSubview:imageV];
    _tableView.tableHeaderView = _headerView;


    //---------下拉刷新----------
    //IOS 6之后，可以为tableView添加一个UIRefreshControl(下啦刷新).关于下啦刷新的布局问题不必要去考虑，系统会自动将他放到表视图里,给他设置一个位置

    //1.创建下拉刷新控件
    re = [[UIRefreshControl alloc]init];
    //2.增加下的title
    re.attributedTitle = [[NSAttributedString alloc]initWithString:@"你倒是用力拉啊"];
    //3.增加一个点击事件
    [re addTarget:self action:@selector(refreshAction:) forControlEvents:UIControlEventValueChanged];

    [_tableView addSubview:re];


}
//获取偏移量 停止刷新
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

//    NSLog(@"targetContentOffset = %f",targetContentOffset->y);
    if (targetContentOffset->y >= 0.0) {
        //结束刷新的方法
        [re endRefreshing];
        re.attributedTitle = [[NSAttributedString alloc]initWithString:@"你倒是用力拉啊"];
    }

}

-(void)refreshAction:(UIRefreshControl*)re{
    //    re.refreshing  这是一个状态值，只读类型的

    //    if (re.refreshing == YES) {
    //        re.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
    //    }

    //(1)开始刷新
    [re beginRefreshing];
    re.attributedTitle = [[NSAttributedString alloc]initWithString:@"满足了^_^ 刷新ing"];
//结束刷新的方法
//     [re endRefreshing];

    
    
}


//收起键盘
-(void)hideKeyboard{

    //让首页的搜索文本框接收通知收起键盘
    [[NSNotificationCenter defaultCenter]postNotificationName:@"touchHomeTableView" object:nil userInfo:nil];

}


//tableView 的几个代理方法

//几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 6;
}

//单元样子
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
#pragma -mark 不采用复用，因为每个单元格都只用一次
    //XIB定制的单元格

//    for (UIView *subview in cell.contentView.subviews) {
//        if (([subview isKindOfClass:[UIButton class]]&&subview.frame.size.height ==  44) || ([subview isKindOfClass:[UILabel class]]&& subview.frame.size.height ==   21)) {
//            [subview removeFromSuperview];
//        }
//    }


    if (indexPath.row == 0) {

//
        HomeTableViewCell  *cell = [[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil][0];
//        UITableViewCell *cell = [[UITableViewCell alloc]init];
//        cell.contentView.backgroundColor = [UIColor redColor]
//        ;

        return cell;
    }

    if (indexPath.row == 5) {

        HomeTableViewCell  *cell = [[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil][2];


        return cell;
    }

    if (indexPath.row == 2) {

        HomeTableViewCell  *cell = [[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil][3];


        return cell;
    }

    if (indexPath.row == 3) {

        HomeTableViewCell  *cell = [[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil][4];


        return cell;
    }

    if (indexPath.row == 4) {

        HomeTableViewCell  *cell = [[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil][5];


        return cell;
    }

    else {

        HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"home_cell"];
        if (cell == nil) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil][1];

        }

        return cell;
    }



}

//组的头视图样子
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HomeSectionHeaderView *sectionHeaderView = [[[NSBundle mainBundle]loadNibNamed:@"HomeSctionHeaderView" owner:nil options:nil]lastObject];
    

    return sectionHeaderView;

}
//单元格的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0 ) {
        return 44;
    }
    if (indexPath.row ==2 ) {
        return 300;
    }

    if (indexPath.row ==5 ) {
        return 43;
    }
    if (indexPath.row ==3 ) {
        return 484;
    }
    if (indexPath.row ==4 ) {
        return 414;
    }
    else {
        return 200;
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
